<?php 
class CompanyModel extends BaseModel {
	public function select_company($group=0,$cname=NULL){
		if($group==0||in_array($group,array(1,2))){
			if($cname!=""){
				$arr['company_name'] = array('like',''.$cname.'%');
				$result = D('company')->where($arr)->order('ctime desc')->findPage(20);
			}else{
				$result = D('company')->order('ctime desc')->findPage(20);
			}
			
		}else{
			if($cname!=""){
				$arr['company_name'] = array('like',''.$cname.'%');
				$arr['company_agent'] = $_SESSION['admin']['aid'];
				$result = D('company')->where($arr)->order('ctime desc')->findPage(20);
			}else{
				$result = D('company')->where('company_agent = '.$_SESSION['admin']['aid'])->order('ctime desc')->findPage(20);
			}
		}
		foreach($result['data'] as $k => &$v){
			$v['menu'] = D('company_menu')->where('cid ='.$v['cid'])->find();
			$menu = D('Menu')->get_menu($v['company_menu']);
			$v['company_menu'] = $menu['m_name'].'（'.$menu['time'].'）';
			$agent = D('User')->get_user($v['company_agent']);
			$v['company_agent'] = $agent['uname'];
		}
		return $result;
	}

	public function select_company_info($map = NULL){
		$result = D('company_info')->where($map)->order('updateline desc')->findPage(20);
		$cname = D('company')->getField('cid,company_name');
		foreach($result['data'] as $k => &$v){
			$v['cname'] = $cname[$v['cid']];
			$v['pic'] = get_img_url($v['license']);
		}
		return $result;
	}
	
	public function get_company($cid){
		$company = D('company')->where('cid = '.$cid)->find();
		$area = json_decode($company['company_area'],true);
		$company['pro'] = getareaname($area['pro']);
		$company['city'] = getareaname($area['city']);
		$company['area'] = getareaname($area['area']);
		$company['company_area'] = $area;
		return $company;
	}
	
	public function get_company_jobs_list($group=0,$map=NULL){
		if($group==0||in_array($group,array(1,2))){
			$jobs = D('hiring')->where($map)->order('updateline desc')->findPage(20);
		}else{
			$companys = D('company')->where('company_agent = '.$_SESSION['admin']['aid'])->select();
			foreach($companys as $k => $v){
				$cids[]=$v['cid'];
			}
			$map['cid'] = array('in',implode(',',$cids));
			$jobs = D('hiring')->where($map)->order('updateline desc')->findPage(20);
		}
		foreach($jobs['data'] as $k => &$v){
			$company = getcompany($v['cid']);
			$v['company'] = $company['company_name'];
		}
		return $jobs;
	}
	
	public function get_company_jobs_list_bykey($company_name,$group=0,$map){
		if($group==0||in_array($group,array(1,2))){
			$companys = D('company')->where('company_name LIKE "%'.$company_name.'%"')->select();
			foreach($companys as $k => $v){
				$cids[]=$v['cid'];
			}
			$map['cid'] = array('in',implode(',',$cids));
			$jobs = D('hiring')->where($map)->select();
		}else{
			$companys = D('company')->where('company_name LIKE "%'.$company_name.'%" AND company_agent = '.$_SESSION['admin']['aid'])->select();
			foreach($companys as $k => $v){
				$cids[]=$v['cid'];
			}
			$map['cid'] = array('in',implode(',',$cids));
			$jobs = D('hiring')->where($map)->select();
		}
		foreach($jobs as $k => &$v){
			$company = getcompany($v['cid']);
			$v['company'] = $company['company_name'];
		}
		return $jobs;
	}

	public function com_count($map){
		$map['is_del'] = 0;
		$count = D('company_info')->where($map)->count();
		return $count;
	}
	public function hiring_count($map){
		$map['is_del'] = 0;
		$count = D('hiring')->where($map)->count();
		return $count;
	}

	public function getinfo($cid){
		$result = D('company_info')->where('cid = '.$cid)->find();
		return $result;
	}

	public function get_manage_list($cid){
		$manage = D('company_manage')->where('cid = '.$cid.' AND is_del = 0')->select();
		return $manage;
	}

	public function list_store($cid){
		$map['cid'] = $cid;
		$map['is_del'] = 0;
		$stores = D('store')->where($map)->findpage(20);
		return $stores;
	} 
}
?>